const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    newGoods: [],
    hotGoods: [],
    //topics: [],
    //brands: [],
    //floorGoods: [],
    banner: [],
    //channel: []
    StorageView: api.StorageView
  },
  onShareAppMessage: function () {
    var userInfo = wx.getStorageSync('userInfo');
    var path="";
      if (userInfo!=null) {
        path = '/pages/index/index?userId=' + userInfo.userId;
      } else {
        path = '/pages/index/index?userId=' + userInfo.userId;
      }
      return {
        title: '您身边的智能机器人一站式购物平台！',
        desc: '小哈微信小程序商城',
        path: path
      }
  },

  getIndexData: function () {
    let that = this;
    util.request(api.IndexUrl).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          newGoods: res.data.newGoodsList,
          hotGoods: res.data.hotGoodsList,
          //topics: res.data.topicList,
          //brands: res.data.brandList,
          //floorGoods: res.data.floorGoodsList,
          banner: res.data.banner,
          //channel: res.data.channel
        });
      }
    });
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options);
   
    var scene = decodeURIComponent(options.scene);
   
    var userId = options.userId;
    if (scene != null && scene != "" && typeof (userId) != "undefined" && scene != "undefined" ){
      console.log(scene)
      userId = scene;
    }
    
    console.log("userId+"+userId)
    if (userId != null && userId != "" && typeof (userId) != "undefined" && userId != "undefined") {
      wx.setStorageSync('parent_user_id', userId);
      //检查是否登录
      if (app.globalData.hasLogin) {
        //已登陆  就检测更新推荐人信息
        util.request(api.memberInfo, { pUserId: userId });

      } else {
        wx.navigateTo({ url: "/pages/auth/login/login" });
      }
    }
    this.getIndexData();
   
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.getIndexData();
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onPullDownRefresh: function(){
    this.getIndexData();
    wx.stopPullDownRefresh();
  }
})
